/*
 * #%L
 * java model
 * %%
 * Copyright (C) 2009 - 2011 prefabware.com Stefan Isele
 * %%
 * All Rights Reserved.
 * This program is dual-licensed under the GNU General Public license
 * or a commercial license.
 * You may select to use one or the other of these licenses.
 * To obtain a commercial license please contact Stefan Isele at www.stefan-isele.de
 * Any modifications to this file must keep this entire header intact.
 * Redistribution, except as permitted by the selected license, is prohibited.
 * #L%
 */
package com.prefabware.jmodel.annotation;

import com.prefabware.jmodel.code.JCodable;
import com.prefabware.jmodel.code.JCodeOptions;
import com.prefabware.jmodel.expression.literal.JArrayLiteral;
import com.prefabware.jmodel.expression.literal.JLiteral;
import com.prefabware.jmodel.expression.literal.JLiteralBase;

public class JAnnotationValue<LITERAL extends JLiteral> implements JCodable {
	private String name;
	private LITERAL literal;

	public static JAnnotationValue<?> create(String name, Class<?> type, Object object) {
		JLiteral literal2 = JLiteralBase.create(object);
		return create(name, literal2);
	}

	public static JAnnotationValue<?> create(String name, JLiteral literal2) {
		if (literal2 instanceof JArrayLiteral) {
			return new JAnnotationValueArray(name, (JArrayLiteral) literal2);
		} else {
			return new JAnnotationValue<JLiteral>(name, literal2);
		}
	};

	public JAnnotationValue(String name, LITERAL literal) {
		super();
		this.name = name;
		this.literal = literal;
	}

	public String getName() {
		return name;
	}

	public Object getValue() {
		return literal.getObject();
	}

	@Override
	public String toString() {
		return asCode(JCodeOptions.DEFAULT);
	}

	@Override
	public String asCode(JCodeOptions options) {
		return this.getName() + " = " + this.literal.asCode(options);
	}

	public LITERAL getLiteral() {
		return literal;
	}
}
